package org.chengn.level_middle.context1423;

public class Context1423 {
    public int maxScore(int[] cardPoints, int k) {
        int sum = 0;
        int winStValue = 0;

        for (int i = 0; i < cardPoints.length; ++i) {
            sum += cardPoints[i];
            if (i == cardPoints.length - k - 1) winStValue = sum;
        }

        if (k == cardPoints.length) return sum;

        int res = sum - winStValue;
        int l = 0, r = cardPoints.length - k;
        while (r < cardPoints.length) {
            winStValue += cardPoints[r++];
            winStValue -= cardPoints[l++];
            res = Math.max(res, sum - winStValue);
        }

        return res;
    }
}
